<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.tip<>'0'";

    	$tips = M()->query($sql);
    	$this->assign('tips',$tips);


       $count =  M('question')->where("tip='0'")->count();

       $Page = new \Think\Page($count,1);

       $show = $Page->show();

       $this->assign('page',$show);


       // var_dump($Page->firstRow,$Page->listRows);


    	$sql = "select t1.*,t2.nickname,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.tip='0' limit ".$Page->firstRow.",".$Page->listRows;

    	$questions = M()->query($sql);
    	$this->assign('questions',$questions);
    	

    	$this->getTopUser();
    	$this->getTopView();
    	$this->getTopAnswer();
        $this->assign('title',"我是一个网站的首页");
        $this->display();
    }

    public function getip()
    {
      header("Content-type: text/html; charset=utf-8");
      $ip = "121.33.63.222";
      $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;

      $url = "http://127.0.0.1/P201701/bbs/index.php/home/reg/checkdata.html";

      $url = "https://www.baidu.com/";


      $url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";

      // $data = file_get_contents($url);
      // 

      // var_dump($data);

      // $obj = json_decode($data,true);

      // var_dump($obj);
      // 
      // 
      //初始化
      $ch = curl_init();
      //设置选项，包括URL
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HEADER, 0);

      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书  
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名 
      //执行并获取HTML文档内容
      $output = curl_exec($ch);
      //释放curl句柄
      curl_close($ch);
      //打印获得的数据
      // var_dump(json_decode($output,true));
      // 
      file_put_contents('./logo.png',  $output);


    }

    public function gettq()
    {
      header("Content-type: text/html; charset=utf-8");
      $host = "http://saweather.market.alicloudapi.com";
      $path = "/spot-to-weather";
      $method = "GET";
      $appcode = "ab7bfa4f7d544241998ea5d8a8ddb028";
      $headers = array();
      array_push($headers, "Authorization:APPCODE " . $appcode);
      $querys = "area=%E6%B3%B0%E5%B1%B1&need3HourForcast=0&needAlarm=0&needHourData=0&needIndex=0&needMoreDay=0";
      $bodys = "";
      $url = $host . $path . "?" . $querys;

      $curl = curl_init();
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($curl, CURLOPT_FAILONERROR, false);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      // curl_setopt($curl, CURLOPT_HEADER, true);
      if (1 == strpos("$".$host, "https://"))
      {
          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
      }
      $data = curl_exec($curl);

      // var_dump($data);

      $res = json_decode($data,true);

      var_dump($res);

      // var_dump($res['showapi_res_body']['list']);
    }
    public function jq()
    {
      header("Content-type: text/html; charset=utf-8");
      
      $q = I('get.q');

      $url = "http://www.tuling123.com/openapi/api";

      $post_data = array ("key" => "facc47b9f3afc7a493121bf6aacab53f","info" => $q,"loc"=>'广州','userid'=>89);

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, $url);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      // post数据
      curl_setopt($ch, CURLOPT_POST, 1);
      // post的变量
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));

      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          'Content-Type: application/json',
          'Content-Length: ' . strlen(json_encode($post_data)))
      );

      $output = curl_exec($ch);

      curl_close($ch);

      //打印获得的数据
  
      var_dump($output);
    }
}